package com.wjc.basemodule.base.adapter

import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import java.util.ArrayList
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.ListAdapter

/**
 * ClassName: com.wjc.basemodule.base.adapter
 * Description: ListAdapter + DataBinding
 *
 */
abstract class BaseDataBindingAdapter<M, B : ViewDataBinding>(diffCallback: DiffUtil.ItemCallback<M>) : ListAdapter<M, RecyclerView.ViewHolder>(diffCallback) {
    private var mOnItemClickListener: OnItemClickListener<M>? = null
    private var mOnItemLongClickListener: OnItemLongClickListener<M>? = null
    var mOnItemChildListener: OnItemChildListener<M>? = null
    fun setOnItemClickListener(onItemClickListener: OnItemClickListener<M>?) {
        mOnItemClickListener = onItemClickListener
    }

    fun setOnItemLongClickListener(onItemLongClickListener: OnItemLongClickListener<M>?) {
        mOnItemLongClickListener = onItemLongClickListener
    }

    fun setOnItemChildListener(onItemChildListener: OnItemChildListener<M>?) {
        mOnItemChildListener = onItemChildListener
    }

    override fun submitList(list: List<M>?) {
        super.submitList(list) { super.submitList(list?.let { ArrayList(it) } ?: ArrayList()) }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val binding = DataBindingUtil.inflate<B>(LayoutInflater.from(parent.context), getLayoutResId(viewType), parent, false)
        val holder = BaseBindingViewHolder(binding.root)
        holder.itemView.setOnClickListener { v: View? ->
            if (mOnItemClickListener != null) {
                val position = holder.bindingAdapterPosition
                if (position < 0) return@setOnClickListener
                mOnItemClickListener!!.onItemClick(getItem(position), position)
            }
        }
        holder.itemView.setOnLongClickListener { v: View? ->
            if (mOnItemLongClickListener != null) {
                val position = holder.bindingAdapterPosition
                if (position < 0) return@setOnLongClickListener true
                mOnItemLongClickListener!!.onItemLongClick(getItem(position), position)
                return@setOnLongClickListener true
            }
            false
        }
        return holder
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val binding = DataBindingUtil.getBinding<B>(holder.itemView)
        if (binding != null) {
            onBindItem(binding, getItem(position), holder)
        }
        binding?.executePendingBindings()
    }

    protected abstract fun getLayoutResId(viewType: Int): Int

    /**
     * 注意：
     * RecyclerView 中的数据有位置改变（比如删除）时一般不会重新调用 onBindViewHolder() 方法，除非这个元素不可用。
     * 为了实时获取元素的位置，我们通过 ViewHolder.getBindingAdapterPosition() 方法。
     *
     * @param binding .
     * @param item    .
     * @param holder  .
     */
    protected abstract fun onBindItem(binding: B, item: M, holder: RecyclerView.ViewHolder)

    class BaseBindingViewHolder internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView)

    interface OnItemClickListener<M> {
        fun onItemClick(item: M, position: Int)
    }

    interface OnItemLongClickListener<M> {
        fun onItemLongClick(item: M, position: Int)
    }

    interface OnItemChildListener<M> {
        fun onItemChildClick(item: M, position: Int, view: View?, type: Int)
    }
}